<template>
  <div class="base-container">
    <t-form ref="form"  :data="queryParams" label-width="80" @submit="()=>{this.pagination.current=1;this.queryParams.pageNum=1;this.getList()}"
            @reset="()=>{this.pagination.current=1;this.getList()}">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">

          <t-form-item label="单位名称" name="deptName">
            <t-input
              v-model="queryParams.deptName"
              clearable
              placeholder="单位名称"
            />
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit"> 查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <div class="btn-group-inline">
      <t-button @click="dialog.header='新增单位项目计划';dialog.objId=null;dialog.open_add_edit=true" variant="outline" theme="primary">
        <t-icon name="add" slot="icon"/>
        新增
      </t-button>
    </div>
    <t-table
      rowKey="id"
      size="small"
      :data="data"
      :columns="columns"
      hover
      :pagination="pagination"
      :loading="dataLoading"
    >
      <template #op="{row}">
        <t-button @click.stop="dialog.header='修改单位项目计划';dialog.objId=row.id;dialog.open_add_edit = true;"
                  size="small" variant="text" class="t-button-link">
          <t-icon name="edit-1" slot="icon"/>
          修改
        </t-button>
        <t-button theme="danger" @click.stop="handleDelete(row)" size="small" variant="text" class="t-button-link">
          <t-icon name="delete" slot="icon"/>
          删除
        </t-button>
      </template>
    </t-table>
    <add-edit-unit-plan @reload="getList" :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_add_edit"
                        v-if="dialog.open_add_edit"></add-edit-unit-plan>
  </div>
</template>

<script>
import AddEditUnitPlan from "./components/add-edit-unit-plan.vue";

export default {
  name: "project-unit-category",
  components:{
    AddEditUnitPlan
  },
  data() {
    return {
      data: [],
      columns: [
        {
          colKey: "deptName",
          title: "单位名称",
          align: "center",
          width: 180,
        },
        // {
        //   colKey: "categoryType",
        //   title: "类型",
        //   align: "center",
        //   width: 100
        // },
        {
          colKey: "startDate",
          title: "申报开始时间",
          align: "center",
          width: 120
        },
        {
          colKey: "endDate",
          title: "申报截止时间",
          align: "center",
          width: 120
        },
        {
          colKey: "dgStartDate",
          title: "大纲开始时间",
          align: "center",
          width: 120
        },
        {
          colKey: "dgEndDate",
          title: "大纲截止时间",
          align: "center",
          width: 120
        },
        {
          colKey: "rwsStartDate",
          title: "任务书开始时间",
          align: "center",
          width: 130
        },
        {
          colKey: "rwsEndDate",
          title: "任务书截止时间",
          align: "center",
          width: 130
        },
        {
          colKey: "ysStartDate",
          title: "验收开始时间",
          align: "center",
          width: 120
        },
        {
          colKey: "ysEndDate",
          title: "验收截止时间",
          align: "center",
          width: 120
        },
        // {
        //   colKey: "createTime",
        //   title: "创建时间",
        //   align: "center"
        // },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 120,
        },
      ],
      dataLoading: false,
      projectCategoryOptions: [],
      dialog:{
        objId: null,
        header: '',
        open_add_edit: false
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptName: '',
        projectCategory: {},
      },
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 50],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination,pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
    }
  },
  created() {
    this.getDeclareTree();
    this.getList();
  },
  methods: {
    getList() {
      this.dataLoading = true;
      this.$api.kjxm.project.deptTimeConfigList(this.queryParams).then(
        (res) => {
          this.data = res.rows;
          this.pagination.total = res.total;
          this.dataLoading = false;
        }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    getDeclareTree() {
      this.$api.kjxm.project.planCategoryList().then(response => {
        this.projectCategoryOptions = this.handleTree(response.data, "id");
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    handleDelete(row){
      let loading = false;
      const confirmDia = this.$dialog.confirm({
        header: "系统提示",
        body: `是否确认删除名称为"${row.deptName}"的数据项？`,
        confirmBtn:{
          content: loading?'提交中...':'提交',
          theme: 'primary',
          loading,
        },
        theme: "warning",
        onConfirm: ({e}) => {
          loading = true;
          confirmDia.update({
            confirmBtn: {
              loading: true,
            },
          })
          this.$api.kjxm.project.deptTimeConfigDel(row.id).then(
            (res) => {
              this.$message.success('删除成功');
              this.getList();
              loading = false;
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            }
          ).catch((e) => {
            loading = false;
            confirmDia.update({
              confirmBtn: {
                loading: false,
              },
            })
            this.$message.error(e.toString());
          });
        },
      })
    },
  }
}
</script>

<style scoped>

</style>
